﻿#include "vtkAutoInit.h"
#include "vtkRenderer.h"
#include "vtkRenderWindow.h"
#include "vtkRenderWindowInteractor.h"
#include "vtkSmartPointer.h"
#include "vtkSphereSource.h"
#include "vtkPolyDataMapper.h"
#include "vtkLODActor.h"
#include "vtkConeSource.h"
#include "vtkGlyph3D.h"
#include "vtkCallbackCommand.h"
#include "vtkCommand.h"
#include "vtkCellPicker.h"
#include "vtkTextMapper.h"
#include "vtkTextProperty.h"
#include "vtkActor2D.h"
#include "vtkCamera.h"
#include "vtkAbstractPicker.h"
#include "vtkPoints.h"
#include "vtkObject.h"
#include "vtkAutoInit.h"
#include "vtkAutoInit.h"
VTK_MODULE_INIT(vtkRenderingOpenGL2);
VTK_MODULE_INIT(vtkInteractionStyle);
VTK_MODULE_INIT(vtkRenderingFreeType);

/// 怎么在actor中显示文字 vtkTextMapper

static vtkSmartPointer<vtkCellPicker> picker1;
static vtkSmartPointer<vtkTextMapper> textMapper;
static vtkSmartPointer<vtkActor2D> textActor;
static vtkSmartPointer<vtkPoints> pickPos;
static vtkSmartPointer<vtkRenderWindow> renWin;
static double *selPt;
static double x;
static double y;
static double xp;
static double yp;
static double zp;

void myCallbackFunction(vtkObject* caller, long unsigned int eventId, void* clientData, void* callData)
{
    picker1 = (vtkCellPicker*)clientData;
    if (picker1->GetCellId() < 0)
    {
        textActor->VisibilityOff();
    }
    else
    {
        selPt = picker1->GetSelectionPoint();
        x = *selPt;
        y = *(selPt + 1);
        pickPos = picker1->GetPickedPositions();
        xp = *(pickPos->GetPoint(0));
        yp = *(pickPos->GetPoint(0)+1);
        zp = *(pickPos->GetPoint(0)+2);
        char str[50];
        snprintf(str, 50, "%f,%f,%f", xp, yp, zp);
        textMapper->SetInput(str);
        textActor->SetPosition(x, y);
        textActor->VisibilityOn();
    }

    renWin->Render();
}

int main()
{
    //# create a sphere source, mapper, and actor
    vtkSmartPointer<vtkSphereSource> sphere = vtkSmartPointer<vtkSphereSource>::New();
    vtkSmartPointer<vtkPolyDataMapper> sphereMapper = vtkSmartPointer<vtkPolyDataMapper>::New();
    sphereMapper->SetInputConnection(sphere->GetOutputPort());
//    sphereMapper->GlobalImmediateModeRenderingOn();
    vtkSmartPointer<vtkLODActor> sphereActor = vtkSmartPointer<vtkLODActor>::New();
    sphereActor->SetMapper(sphereMapper);

    //# create the spikes by glyphing the sphere with a cone.Create the mapper
    //# and actor for the glyphs.
//    vtkSmartPointer<vtkConeSource> cone = vtkSmartPointer<vtkConeSource>::New();
//    vtkSmartPointer<vtkGlyph3D> glyph = vtkSmartPointer<vtkGlyph3D>::New();
//    glyph->SetInputConnection(sphere->GetOutputPort());
//    glyph->SetSourceConnection(cone->GetOutputPort());
//    glyph->SetVectorModeToUseNormal();
//    glyph->SetScaleModeToScaleByVector();
//    glyph->SetScaleFactor(0.25);
//    vtkSmartPointer<vtkPolyDataMapper> spikeMapper = vtkSmartPointer<vtkPolyDataMapper>::New();
//    spikeMapper->SetInputConnection(glyph->GetOutputPort());
//    vtkSmartPointer<vtkLODActor> spikeActor = vtkSmartPointer<vtkLODActor>::New();
//    spikeActor->SetMapper(spikeMapper);

    //# Create a cell picker.
    vtkSmartPointer<vtkCellPicker> picker = vtkSmartPointer<vtkCellPicker>::New();
    vtkSmartPointer<vtkCallbackCommand> annotatePick = vtkSmartPointer<vtkCallbackCommand>::New();
    annotatePick->SetCallback(myCallbackFunction);
    annotatePick->SetClientData(picker);
    picker->AddObserver(vtkCommand::EndPickEvent, annotatePick);

    //# Create a text mapper and actor to display the results of picking.
    textMapper = vtkSmartPointer<vtkTextMapper>::New();
    textMapper->GetTextProperty()->SetFontFamilyToArial();
    textMapper->GetTextProperty()->SetFontSize(10);
    textMapper->GetTextProperty()->BoldOn();
    textMapper->GetTextProperty()->ShadowOn();
    textMapper->GetTextProperty()->SetColor(1, 0, 0);
    textActor = vtkSmartPointer<vtkActor2D>::New();
    textActor->VisibilityOff();
    textActor->SetMapper(textMapper);

    //Create the Renderer, RenderWindow, RenderWindowInteractor
    vtkSmartPointer<vtkRenderer> ren1 = vtkSmartPointer<vtkRenderer>::New();
    renWin = vtkSmartPointer<vtkRenderWindow>::New();
    renWin->AddRenderer(ren1);
    vtkSmartPointer<vtkRenderWindowInteractor> iren = vtkSmartPointer<vtkRenderWindowInteractor>::New();
    iren->SetRenderWindow(renWin);
    iren->SetPicker(picker);

    //Add the actors to the renderer, set the background and size
    ren1->AddActor2D(textActor);
    ren1->AddActor(sphereActor);
    //ren1->AddActor(spikeActor);
    ren1->SetBackground(1, 1, 1);
    renWin->SetSize(300, 300);
    ren1->ResetCamera();
    ren1->GetActiveCamera()->Zoom(1.4);

    //# Pick the cell at this location.
    picker->Pick(85.0, 126.0, 0, ren1);

    iren->Start();
    return 0;
}
